<?php namespace Home\Controller;
use Common\Model\Addaddress;
use Common\Model\Orderd;
use Common\Model\Orderdnow;
use Common\Model\OrderList;
use Common\Model\OrderListnow;
use Hdphp\Controller\Controller;

class OrderController extends Controller{
    public function index(){
//        获得购物车提交过来的数据
        $allData=Cart::getAllData();
        if(!is_null($_SESSION['cart']['goods'])){
            $allNum=Cart::getTotalNums();
            $allpri=Cart::getTotalPrice();
            //        获取订单中商品所省的钱的总和，方便计算省的价钱
            $shengsum='';
            foreach($_SESSION['cart']['goods'] as $u){
                $shengsum+=$u['sheng']*$u['num'];
            }
        }else{
            $allNum=0;
            $allpri=0.00;
            $shengsum=0.00;
        }
        View::with('allNum',$allNum);
        View::with('allData',$allData);
        View::with('allpri',$allpri);
        View::with('shengsum',$shengsum);
        //        显示用户收货地址
        $uid=$_SESSION['userinfo']['uid'];
        $addaddressModel=new Addaddress();
        $addressData=$addaddressModel->where("shop_user_uid={$uid}")->get();
        View::with('addressData',$addressData);
//        p($allData);die;
        View::make();
    }

    //    用户地址选择
    public function chooseAddress(){
        $rid=Q('post.r');
        $uid=$_SESSION['userinfo']['uid'];
        $addaddressModel=new Addaddress();
        $addaddressModel->where("shop_user_uid={$uid}")->update(['restate'=>0]);
        $addaddressModel->where("rid={$rid} AND shop_user_uid={$uid}")->update(['restate'=>1]);
//        p($rid);
    }

    //    购物车订单页面用户添加地址
    public function addAddress(){
        $addaddressModel=new Addaddress();
        if(IS_POST){
            if(!$addaddressModel->store()){
                View::error($addaddressModel->getError());
            }else{
                View::success('添加成功',U('index'));
            }
        }
        View::make();
    }
    //    立即购买订单页面用户添加地址
    public function addAddressp(){
        $addaddressModel=new Addaddress();
        if(IS_POST){
            if(!$addaddressModel->store()){
                View::error($addaddressModel->getError());
            }else{
                View::success('添加成功',U('ordernow'));
            }
        }
        View::make();
    }
    //    购物车订单页面用户地址删除
    public function delAddress(){
        $rid=Q('get.rid',0,'intval');
        $addaddressModel=new Addaddress();
        $addaddressModel->where("rid={$rid}")->delete();
        View::success('删除成功',U('index'));
    }
    //    立即购买订单页面用户地址删除
    public function delAddressp(){
        $rid=Q('get.rid',0,'intval');
        $addaddressModel=new Addaddress();
        $addaddressModel->where("rid={$rid}")->delete();
        View::success('删除成功',U('ordernow'));
    }
//    异步接受提交订单的数据
    public function orderd(){
//        p(Q('post.'));
        $orderdModel=new Orderd();
        $orderliModel=new OrderList();
        if(IS_AJAX){
            $orderdModel->store();
            $orderliModel->store();
        }
    }
//    支付页面
    public function orderpay(){
//        获取要支付的总金额
//        p($_SESSION);die;
        $orderModel=new Orderd();
//        订单号
        $ordernum=$_SESSION['ordernum'];
//        订单金额
        $orderPri=$orderModel->where("ordernum='{$ordernum}'")->pluck('orderpri');
//        订单时间
        $ordertime=$orderModel->where("ordernum='{$ordernum}'")->pluck('ordertime');
        $orderpayData=$orderModel->join('orderlist ol','oid','=','ol.shop_orderd_oid')->join('commodity cd','ol.shop_commodity_coid','=','cd.coid')->where("ordernum='{$ordernum}'")->lists('coname');
//        p($orderpayData);die;
        View::with('orderpayData',$orderpayData);
        View::with('ordernum',$ordernum);
        View::with('orderPri',$orderPri);
        View::with('ordertime',$ordertime);
        View::make();
    }


//    立即购买跳转的订单信息
    public function ordernow(){
        //        显示用户收货地址
//        p($_SESSION['buynow']);
        $uid=$_SESSION['userinfo']['uid'];
        $addaddressModel=new Addaddress();
        $addressData=$addaddressModel->where("shop_user_uid={$uid}")->get();
//        显示立即购买的数据
        $buyData=$_SESSION['buynow'];
        View::with('buyData',$buyData);
        View::with('addressData',$addressData);
        View::make();
    }

//    立即购买异步提交过来的数据
    public function orderdnow(){
//        p(Q('post.'));p($_SESSION['buynow']);die;
        $ordernowModel=new Orderdnow();
        $orderlinowModel=new OrderListnow();
        if(IS_AJAX){
            $ordernowModel->store();
            $orderlinowModel->store();
        }
    }

    //    支付页面
    public function ordernowpay(){
//        p($_SESSION['buynow']);
//        获取要支付的总金额
        $orderModel=new Orderd();
//        订单号
        $ordernum=$_SESSION['ordernumnow'];
//        订单金额
        $orderPri=$orderModel->where("ordernum='{$ordernum}'")->pluck('orderpri');
//        订单时间
        $ordertime=$orderModel->where("ordernum='{$ordernum}'")->pluck('ordertime');
        $orderpayData=$orderModel->join('orderlist ol','oid','=','ol.shop_orderd_oid')->join('commodity cd','ol.shop_commodity_coid','=','cd.coid')->where("ordernum='{$ordernum}'")->lists('coname');
//        p($ordertime);die;
        View::with('orderpayData',$orderpayData);
        View::with('ordernum',$ordernum);
        View::with('orderPri',$orderPri);
        View::with('ordertime',$ordertime);
        View::make();
    }

//    点击立即购买支付按钮
    public function paynow(){
        $ordernum=Q('get.ordernum');
        $orderModel=new Orderd();
        $orderModel->where("ordernum='{$ordernum}'")->update(['orderstate'=>2]);
        View::success('支付成功',U('Personal/index'));
    }

//    点击加入购物车订单支付按钮
    public function paycart(){
        $ordernum=Q('get.ordernum');
        $orderModel=new Orderd();
        $orderModel->where("ordernum='{$ordernum}'")->update(['orderstate'=>2]);
        View::success('支付成功',U('Personal/index'));
    }
}